/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.ec;

import it.polimi.registration.business.security.control.PasswordEncrypter;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Egzon
 */
@Entity
@Table(name = "User", uniqueConstraints=@UniqueConstraint(columnNames={"userID", "userEmail"}))
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findByUserID", query = "SELECT u FROM User u WHERE u.userID = :userID"),
    @NamedQuery(name = "User.findByGroupname", query = "SELECT u FROM User u WHERE u.groupname = :groupname"),
    @NamedQuery(name = "User.findByUserCalendarPrivacy", query = "SELECT u FROM User u WHERE u.userCalendarPrivacy = :userCalendarPrivacy"),
    @NamedQuery(name = "User.findByUserEmail", query = "SELECT u FROM User u WHERE u.userEmail = :userEmail"),
    @NamedQuery(name = "User.findByUserLastName", query = "SELECT u FROM User u WHERE u.userLastName = :userLastName"),
    @NamedQuery(name = "User.findByUserName", query = "SELECT u FROM User u WHERE u.userName = :userName"),
    @NamedQuery(name = "User.findByUserPassword", query = "SELECT u FROM User u WHERE u.userPassword = :userPassword"),
    @NamedQuery(name = "User.findByUserUsername", query = "SELECT u FROM User u WHERE u.userUsername = :userUsername")})
public class User implements Serializable {
    @JoinColumn(name = "userAddress", referencedColumnName = "addressID")
    @ManyToOne
    private Address userAddress;
    private static final long serialVersionUID = 1L;
    
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "userID")
    private Integer userID;
    @Size(max = 255)
    @Column(name = "Groupname")
    private String groupname;
    @Column(name = "userCalendarPrivacy")
    private Boolean userCalendarPrivacy;
    @Size(max = 255)
    @Column(name = "userEmail")
    @Id
    private String userEmail;
    @Size(max = 255)
    @Column(name = "userLastName")
    private String userLastName;
    @Size(max = 255)
    @Column(name = "userName")
    private String userName;
    @Size(min = 1, max = 255)
    @Column(name = "userPassword")
    private String userPassword;
    @Size(max = 255)
    @Column(name = "userUsername")
    private String userUsername;

    public User() {
    }

    public User(Integer userID) {
        this.userID = userID;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public String getGroupname() {
        return groupname;
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public Boolean getUserCalendarPrivacy() {
        return userCalendarPrivacy;
    }

    public void setUserCalendarPrivacy(Boolean userCalendarPrivacy) {
        this.userCalendarPrivacy = userCalendarPrivacy;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = PasswordEncrypter.encryptPassword(userPassword);
    }

    public String getUserUsername() {
        return userUsername;
    }

    public void setUserUsername(String userUsername) {
        this.userUsername = userUsername;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userID != null ? userID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.userID == null && other.userID != null) || (this.userID != null && !this.userID.equals(other.userID))) {
            
            if((this.userEmail == null && other.userEmail != null) || (this.userEmail != null & !this.userEmail.equals(other.userEmail))){
            return false;
        }
            return false;
        }
        
        return true;
    }

    @Override
    public String toString() {
        return "it.polimi.registration.business.security.ec.User[ userID=" + userID + " ]";
    }

    public Address getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(Address userAddress) {
        this.userAddress = userAddress;
    }
    
}
